<?php
/**
* @filename source_module.php 2013-11-11 02:10:56x
* @author Huming Xu <huming17@126.com>
* @version 1.0.0
* @copyright DZF (c) 2013, Huming Xu
*/

class ctrl_course_evaluate extends course_evaluate{
    public $info_array = array();
	public $page_array = array();
	public $tree_array = array();
    
    static function &instance() {
		static $object;
		if(empty($object)) {
			$object = new self();
		}
		return $object;
	}
    /*
	*	取出一条信息
	*/
	public function do_detail($cm_id='') {
		$detail = array();
        $cm_id = isset($_REQUEST['cm_id']) ? $_REQUEST['cm_id'] : '';
		$detail = $this->one_info('course_main','cm_id',$cm_id);
        //DEBUG 调试输出
        include template('course/main/detail');
	}
	
	/*
	 *	取出评价列表信息
	 */
	public function do_detail_list() {	
		global $_G;
		$detail = array();
		$page = max(1, intval($_GET['page']));
		$perpage = $limit = isset($_REQUEST['limit']) && !empty($_REQUEST['limit']) ? $_REQUEST['limit'] : '10';
		$start=(($page-1) * $perpage);
		$wheresql = "";
		$wheresql .= " AND ce.isdelete = 0";
		$api=isset($_REQUEST['api']) ? $_REQUEST['api']:'json';
		
		//课程或课时id
		$te_key_id= isset($_REQUEST['te_key_id']) && !empty($_REQUEST['te_key_id']) ? $_REQUEST['te_key_id'] : '';
		
		$te_type= isset($_REQUEST['te_type']) && !empty($_REQUEST['te_type']) ? $_REQUEST['te_type'] : '';
		if($te_key_id&&$te_type){
			$wheresql .= " AND ce.te_key_id = ".$te_key_id;
		}
		if($te_type){
			$wheresql .= " AND ce.te_type = ".$te_type;
		}
		//是否审核
		$te_check= isset($_REQUEST['te_check']) && !empty($_REQUEST['te_check']) ? $_REQUEST['te_check'] : '';
		if($te_check){
			$wheresql .= " AND ce.te_check = ".$te_check;
		}
		//评价内容
		$te_content= isset($_REQUEST['te_content']) && !empty($_REQUEST['te_content']) ? $_REQUEST['te_content'] : '';
		if($te_content){		
			$wheresql .= " AND ce.te_content LIKE '%".$te_content."%'";
		}
		$orderby = " ORDER BY ce.create_dateline DESC,ce.te_id DESC ";	
		//DEBUG 初始化请求请求获取一页列表数据的参数
		$page_condition=array(
				'page' => $page,//int 请求页面 页码
				'limit' => $limit,//int 每页请求个数
				'perpage' => $perpage,//int 每页显示个数
				'wheresql' => $wheresql,//string //条件SQL语句
				'orderby' => $orderby,//string 排序规则
		);		
		$page_result = $this->detail_list('course_evaluate',$page_condition);	
	
		//DEBUG 调试输出
		if($page_result){
			$return['code'] = '0';
			$return['data']=$page_result;
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}else{
			$return['code'] = '1';
			$return['data']=array();
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
				
	}
    
	/*
	 *  添加课程
	 */
	public function do_add(){				
		global $_G;	
		$res_data=array();
		$client=isset($_REQUEST['client']) ? $_REQUEST['client']:'pc';
		$course_evaluate = isset($_REQUEST['course_evaluate']) && !empty($_REQUEST['course_evaluate']) ? $_REQUEST['course_evaluate']:'';		
				
		//TODO 后端字段校验 根据具体业务逻辑添加
		//DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
		foreach($course_evaluate AS $key => $value){
			if(is_array($value) && !empty($value)){
				$course_evaluate[$key] = implode(",", $value);
			}
		}
		$course_evaluate['create_dateline'] = TIMESTAMP;
		$course_evaluate['modify_dateline'] = TIMESTAMP;
		$course_evaluate['user_id'] = $_G['user_id'];
		$course_evaluate['te_check'] = '0';
		
		$insert_data = $this->add('course_evaluate',$course_evaluate);		
		if($insert_data){										
			$return['code'] = '0';
			$return['data']=$insert_data;		
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}else{
			$return['code'] = '1';
			$return['data']=array();
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
		
	}
    
	/*
	*  编辑课时信息
	*/
	public function do_edit() {
        global $_G;		
		$course_hour = isset($_REQUEST['course_hour']) && !empty($_REQUEST['course_hour']) ? $_REQUEST['course_hour']:'';
        $ch_id = isset($_REQUEST['ch_id']) ? $_REQUEST['ch_id'] : '';
        if($ch_id){                      
            //DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮          
            foreach($course_hour AS $key => $value){
            	if(is_array($value) && !empty($value)){
            		$course_hour[$key] = implode(",", $value);
            	}
            }         
            $course_hour['modify_dateline'] = TIMESTAMP;                      
            $where = array('ch_id'=>$ch_id);
            $effect_row = $this->edit('course_hour',$course_hour,$where);
            if($effect_row){
                $return['code'] = '0';
				$return['data']=$course_hour;
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
            }else{
            	$return['code'] = '1';
            	$return['data']=array();
            	echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
            }            
        }else{
            	$return['code'] = '3';
            	$return['data']=array();
            	echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
        }
	}
	
	/*
	 *  审核评价
	 */
	public function do_check() {
		global $_G;
		$course_check = isset($_REQUEST['course_check']) && !empty($_REQUEST['course_check']) ? $_REQUEST['course_check']:'';
		$te_id = isset($_REQUEST['te_id']) ? $_REQUEST['te_id'] : '';
		$te_check = isset($_REQUEST['te_check']) ? $_REQUEST['te_check'] : '0';
		if($te_id){
			//DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
			foreach($course_check AS $key => $value){
				if(is_array($value) && !empty($value)){
					$course_check[$key] = implode(",", $value);
				}
			}
			$course_check['te_check'] = $te_check;
			$course_check['modify_dateline'] = TIMESTAMP;
			$course_check['te_check_user_id'] = $_G['user_id'];
			$where = array('te_id'=>$te_id);
			$effect_row = $this->edit('course_evaluate',$course_check,$where);
			if($effect_row){
				$return['code'] = '0';
				$return['data']=$course_check;
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}else{
				$return['code'] = '1';
				$return['data']=array();
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}
		}else{
			$return['code'] = '3';
			$return['data']=array();
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
	}
    
	/*
	*  删除课时信息
	*/
	public function do_delete() {
        global $_G;
        $te_id = isset($_REQUEST['te_id']) ? $_REQUEST['te_id'] : '';      
        if($te_id){
            $effect_row = $this->delete("course_evaluate", array("te_id" => $te_id));
            if($effect_row){
                $return['code'] = '0';
				$return['data']=array();
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
            }else{           	
                $return['code'] = '1';
            	$return['data']=array();
            	echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
            }
        }else{
            $return['code'] = '3';
            $return['data']=array();
            echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
        }
    }
	/*
	*  系统信息列表
	*/
	public function do_index(){
        global $_G;
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start=(($page-1) * $perpage);
        $wheresql = "";
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        if($keyword){
            $wheresql = " AND  LIKE '%".$keyword."%' ";
        }
        $orderby = " ORDER BY cm_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition=array(
            'page' => $page,//int 请求页面 页码
            'limit' => $limit,//int 每页请求个数
            'perpage' => $perpage,//int 每页显示个数
            'wheresql' => $wheresql,//string //条件SQL语句 
            'orderby' => $orderby,//string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
        $page_result = array(
            //int 返回结果总数
            'total_rows' => $total_rows,
            //array 一页数据数组
            'page_data' => $page_data 
        );
        */
        $page_result = $this->index('course_main',$page_condition);
        $multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=course&action=main&do=index&keyword=".$keyword);
		include template('course/main/index');	
	}

   /**
     * 接口 AJAX或外部请求检索数据
     *
     * @param $return_type string json/xml.
     *
     * @return print string json/xml.
     */
	public function do_api(){
        $return = '';
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start=(($page-1) * $perpage);
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        $return_type = isset($_REQUEST['return_type']) ? $_REQUEST['return_type'] : 'json';
        $wheresql = "";
        if($keyword){
            $wheresql = " WHERE title LIKE '%".$keyword."%'";
        }
        $orderby = " ORDER BY cm_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition=array(
            'page' => $page,//int 请求页面 页码
            'limit' => $limit,//int 每页请求个数
            'perpage' => $perpage,//int 每页显示个数
            'wheresql' => $wheresql,//string //条件SQL语句 
            'orderby' => $orderby,//string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
        $page_result = array(
            //int 返回结果总数
            'total_rows' => $total_rows,
            //array 一页数据数组
            'page_data' => $page_data 
        );
        */
        $page_result = $this->api('course_main',$page_condition);      
        if('json'==$return_type){
            $return = json_encode($page_result);	
        }elseif('xml'==$return_type){
            $return = array2xml($page_result);
        }
        //DEBUG 接口响应直接输出返回
		echo $return;
	}
}
?>